package org.vafada.swtcalendar;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.RowLayout;

import org.eclipse.swt.widgets.Display;

import org.eclipse.swt.widgets.Shell;

import org.vafada.swtcalendar.SWTCalendar;

import org.vafada.swtcalendar.SWTCalendarListener;
import com.swtdesigner.SWTResourceManager;

import java.util.Calendar;

import java.util.Date;

public class SWTCalendarDialog {

	private Shell shell;

	private SWTCalendar swtcal;

	private Display display;

	public SWTCalendarDialog(Display display) {
		this.display = display;
		shell = new Shell(display, SWT.APPLICATION_MODAL | SWT.CLOSE);
		shell.setFont(SWTResourceManager.getFont("", 12, SWT.NONE));
		shell.setLayout(new RowLayout());
		swtcal = new SWTCalendar(shell);
	}

	public void open() {

		shell.pack();
		centreShell(shell);
		shell.open();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();

		}
	}

	public Calendar getCalendar() {
		return swtcal.getCalendar();
	}

	public void setDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		swtcal.setCalendar(calendar);
	}

	public void addDateChangedListener(SWTCalendarListener listener) {
		swtcal.addSWTCalendarListener(listener);
	}

	public void centreShell(Shell shell) {
		Rectangle pDisplayBounds = shell.getDisplay().getBounds();
		Rectangle pShellBounds = shell.getBounds();

		int nLeft = (pDisplayBounds.width - pShellBounds.width) / 2;
		int nTop = (pDisplayBounds.height - pShellBounds.height) / 2;

		shell.setBounds(nLeft, nTop, pShellBounds.width, pShellBounds.height);
	}
}
